var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
import React, { createContext, useContext, useReducer } from 'react';
import { ConfigReducer } from '../reducers/ConfigReducer';
import {
  SET_ACTIVE_SCHEMA,
  SET_ACTIVE_SCHEMA_CURRENT,
  SET_FORCE_RENDER,
  SET_ITEM_STORE,
  SET_SCHEMA_LIST,
  SET_SCHEMA_MODAL
} from '../constants';
var initialState = {
  activeSchema: '',
  schemaList: [],
  schemaListOptions: [],
  onGetSchemaModel: undefined,
  forceRender: function () {},
  itemStore: {}
};
var ConfigContext = createContext(initialState);
var ConfigContextProvider = function (props) {
  var _a = useReducer(ConfigReducer, initialState),
    state = _a[0],
    dispatch = _a[1];
  var actions = {
    setActiveSchema: function (data) {
      dispatch({ type: SET_ACTIVE_SCHEMA, data: data });
    },
    setActiveSchemaCurrent: function (data) {
      dispatch({ type: SET_ACTIVE_SCHEMA_CURRENT, data: data });
    },
    setSchemaList: function (listFunc) {
      var newList = [];
      if (typeof listFunc === 'function') {
        newList = listFunc(state.schemaList);
      } else {
        newList = listFunc;
      }
      dispatch({ type: SET_SCHEMA_LIST, data: newList });
    },
    setSchemaModel: function (data) {
      dispatch({ type: SET_SCHEMA_MODAL, data: data });
    },
    setForceRender: function (func) {
      dispatch({ type: SET_FORCE_RENDER, data: func });
    },
    setItemStore: function (newObj) {
      console.log('context set', newObj);
      dispatch({ type: SET_ITEM_STORE, data: newObj });
    },
    editItemStore: function (field, storeKey) {},
    deleteItemStoreItem: function (key) {
      delete state.itemStore[key];
    }
  };
  return React.createElement(
    ConfigContext.Provider,
    { value: __assign(__assign({}, state), actions) },
    props.children
  );
};
export var useConfigContext = function () {
  return useContext(ConfigContext);
};
export { ConfigContext, ConfigContextProvider };
